package com.ohs.ohsoa.utils.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ohs.ohsoa.utils.bean.responseBean.ResultBean;

/**
 * 全局异常处理类
 *
 * @param
 * @return
 * @author mwx
 * @date 2021年9月23日
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResultBean<?> bizExceptionHandler(HttpServletRequest req, BizException e) {
        logger.error(e.getErrorMsg(),e);
        return ResultBean.fail(e.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResultBean<?> exceptionHandler(HttpServletRequest req, NullPointerException e) {
        return ResultBean.fail("システムエラーが発生しました。");
    }

    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultBean<?> exceptionHandler(HttpServletRequest req, Exception e) {
        return ResultBean.fail(e.getMessage());
    }
}
